// FilterDialog.cpp : implementation file
//

#include "stdafx.h"
//#include "..\include\UMyGrid9.h"
#include "FilterDialog.h"


// CFilterDialog dialog

IMPLEMENT_DYNAMIC(CFilterDialog, CDialog)

CFilterDialog::CFilterDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CFilterDialog::IDD, pParent)
{

}

CFilterDialog::~CFilterDialog()
{
}

void CFilterDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TREE_VALUE, m_Tree);
}


BEGIN_MESSAGE_MAP(CFilterDialog, CDialog)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_VALUE, &CFilterDialog::OnTvnSelchangedTreeValue)
	ON_NOTIFY(NM_CLICK, IDC_TREE_VALUE, &CFilterDialog::OnNMClickTreeValue)
	ON_BN_CLICKED(IDOK, &CFilterDialog::OnBnClickedOk)
END_MESSAGE_MAP()


// CFilterDialog message handlers

BOOL CFilterDialog::OnInitDialog()
{
	CDialog::OnInitDialog();

	TVINSERTSTRUCT tvInsert;
	tvInsert.hParent = NULL;
	tvInsert.hInsertAfter = NULL;
	tvInsert.item.mask = TVIF_TEXT;
	tvInsert.item.pszText = _T("ȫѡ");

	HTREEITEM hItem=m_Tree.InsertItem(TVIF_TEXT,_T("ȫѡ"),0,0,0,0,0,NULL,NULL);
	m_Tree.SetItemData(hItem,0);

	for(int i=0;i<m_aryValue.GetSize();++i)
	{
		hItem=m_Tree.InsertItem(TVIF_TEXT,m_aryValue[i],0,0,0,0,0,NULL,NULL);
		m_Tree.SetItemData(hItem,i+1);
	}

	CRect rect;
	GetClientRect(&rect);
	MoveWindow(m_MovePt.x,m_MovePt.y,205,320);
	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CFilterDialog::SetMousePoint(const POINT pt)
{
	m_MovePt.x=pt.x;
	m_MovePt.y=pt.y;
}

void CFilterDialog::AddValue(LPCTSTR lpValue)
{
	for(int i=0;i<m_aryValue.GetSize();++i)
	{
		if (m_aryValue[i].CompareNoCase(lpValue)==0) return;
	}

	m_aryValue.Add(lpValue);
}

void CFilterDialog::OnTvnSelchangedTreeValue(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
	*pResult = 0;
}

void CFilterDialog::OnNMClickTreeValue(NMHDR *pNMHDR, LRESULT *pResult)
{
	*pResult = 0;

	UINT   flag; 
	CPoint   point; 
	GetCursorPos(&point); 
	m_Tree.ScreenToClient(&point); 
	
	HTREEITEM   hItem   =   m_Tree.HitTest(point,&flag); 
	if (!hItem) return;

	if(flag & TVHT_ONITEMSTATEICON) 
	{
		m_Tree.SelectItem(hItem);
		int nData=m_Tree.GetItemData(hItem);
		if (nData!=0) return;
		BOOL bCheck=m_Tree.GetCheck(hItem);

		hItem=m_Tree.GetRootItem();
		while(hItem)
		{
			hItem=m_Tree.GetNextItem(hItem,TVGN_NEXT);
			if (hItem)
				m_Tree.SetCheck(hItem,bCheck?0:1);
		}
		
	}
}

void CFilterDialog::OnBnClickedOk()
{
	HTREEITEM hItem=m_Tree.GetRootItem();
	CString strValue;
	while(hItem)
	{
		hItem=m_Tree.GetNextItem(hItem,TVGN_NEXT);
		if (hItem && m_Tree.GetCheck(hItem))
		{
			strValue=m_Tree.GetItemText(hItem);
			m_aryFilter.Add(strValue);
		}
	}

	OnOK();
}
